package com.example.oj.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.oj.entity.Problem;
import com.example.oj.entity.ProblemData;
import com.example.oj.mapper.ProblemDataMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ProblemDataService extends ServiceImpl<ProblemDataMapper, ProblemData> {
    @Resource
    ProblemDataMapper problemDataMapper;

    public Page<ProblemData> SelectPage(Integer pageNum, Integer pageSize, String search){
        LambdaQueryWrapper<ProblemData> wrapper = Wrappers.lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(ProblemData::getTitle, search).or
                    (query->{query.like(ProblemData::getId, search);});
        }
        Page<ProblemData> Page = new Page<>(pageNum, pageSize);
        Page.addOrder(OrderItem.asc("id")); // 按照 id 排序
        return problemDataMapper.selectPage(Page, wrapper);
    }
    public ProblemData getByTitle(String title){
        LambdaQueryWrapper<ProblemData> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ProblemData::getTitle, title);
        return problemDataMapper.selectOne(wrapper);
    }
}
